package ar.com.ford.it.fullEconomics.view.form;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import org.apache.struts.validator.ValidatorForm;

import ar.com.ford.it.fullEconomics.persistence.dto.Teesc02Coststvers;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesc03Subcoststr;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesc07Subcstitem;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesf01Fulleconom;
import ar.com.ford.it.fullEconomics.struct.FullEconomicsStruct;
import ar.com.ford.it.fullEconomics.struct.FullEconomicsTotalStruct;
import ar.com.ford.it.fullEconomics.util.FullEconomicsDateUtil;

public class FullEconomicsForm extends ValidatorForm {
    private String section;
    private String vehicleLineSel;
    private String evolveFromSel;
    private String evolveFromYear;
    private String evolveToSel;
    private String evolveToYear;
    private String mixDate;
    private String mixYear;
    private String id;
    private Collection<FullEconomicsStruct> colFullEconomicsStruct;
    private String fullEconSelNum;
    private Teesc02Coststvers costStrVersion;
    private Timestamp econLevel1;
    private Timestamp econLevel2;
    private String econLevel1Forecasted;
    private String econLevel2Forecasted;
    private FullEconomicsTotalStruct totals;
    private Vector<String> altValue2Coll;
    private Teesf01Fulleconom fullEconomic;
    private String comments;
    private String createdDate;
    private String lastModifiedDate;
    private String proposedDate;
    private String approvedDate;
    
    private Map<Teesc03Subcoststr, Collection<Teesc07Subcstitem>> mapaNodosNiveles;
    private Boolean activacionNiveles;

    public void resetForm() {
        this.section = null;
        this.id = null;
        this.colFullEconomicsStruct = null;
        this.fullEconSelNum = null;
        this.costStrVersion = null;
        this.econLevel1 = null;
        this.econLevel2 = null;
        this.econLevel1Forecasted = null;
        this.econLevel2Forecasted = null;
        totals = null;
        this.altValue2Coll = new Vector<String>();
        this.fullEconomic = null;
        mapaNodosNiveles = new HashMap<Teesc03Subcoststr, Collection<Teesc07Subcstitem>>();
        activacionNiveles = false;
    }

    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {

        ActionErrors ae = new ActionErrors();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);
        if (this.getSection().equalsIgnoreCase("calculateAltValues")) {
            for (String value : this.altValue2Coll) {
                try {
                    if (new BigDecimal(value) == null) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage(
                                        "fullEconomics.error.altValue"));
                        break;
                    }
                } catch (NumberFormatException e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "fullEconomics.error.altValue"));
                    break;
                }
            }
        }
        return ae;
    }

    public void setFullEconomicDetails() {
        this.setCostStrVersion(this.getFullEconomic().getTeesc02Coststvers());
        this.setEconLevel1(this.getFullEconomic()
                .getEesf01FulleEcolevel1DateY());
        this.setEconLevel2(this.getFullEconomic()
                .getEesf01FulleEcolevel2DateY());
        this.setVehicleLineSel(this.getFullEconomic().getNcas012VehicleVw()
                .getCas012VehicleCodeC());
        this.setEvolveFromSel(new Integer(FullEconomicsDateUtil.getMonth(this
                .getFullEconomic().getEesf01FulleEcolevel1DateY())).toString());
        this.setEvolveToSel(new Integer(FullEconomicsDateUtil.getMonth(this
                .getFullEconomic().getEesf01FulleEcolevel2DateY())).toString());
        this.setEvolveFromYear(FullEconomicsDateUtil.getYear(this
                .getFullEconomic().getEesf01FulleEcolevel1DateY()));
        this.setEvolveToYear(FullEconomicsDateUtil.getYear(this
                .getFullEconomic().getEesf01FulleEcolevel2DateY()));
        
        if(this.getFullEconomic().getEesf01MixEffectiveDateY() != null){
            this.setMixYear(FullEconomicsDateUtil.getYear(this.getFullEconomic()
                    .getEesf01MixEffectiveDateY()));
            this.setMixDate(new Integer(FullEconomicsDateUtil.getMonth(this.getFullEconomic()
                    .getEesf01MixEffectiveDateY())).toString());
        }
        Calendar createDateCal = Calendar.getInstance();
        Calendar updateDateCal = Calendar.getInstance();
        Calendar proposeDateCal = Calendar.getInstance();
        Calendar aproveDateCal = Calendar.getInstance();
        createDateCal
                .setTimeInMillis(fullEconomic.getEesf01CreateS().getTime());
        this.setCreatedDate(FullEconomicsDateUtil.format(createDateCal));
        updateDateCal.setTimeInMillis(fullEconomic.getEesf01LastUpdtS()
                .getTime());
        this.setLastModifiedDate(FullEconomicsDateUtil.format(updateDateCal));
        if (fullEconomic.getEesf01FulleProposalS() != null) {
            proposeDateCal.setTimeInMillis(fullEconomic
                    .getEesf01FulleProposalS().getTime());
            this.setProposedDate(FullEconomicsDateUtil.format(proposeDateCal));
        }
        if (fullEconomic.getEesf01FulleApprovalS() != null) {
            aproveDateCal.setTimeInMillis(fullEconomic
                    .getEesf01FulleApprovalS().getTime());
            this.setApprovedDate(FullEconomicsDateUtil.format(aproveDateCal));
        }
    }

    public String getSelectedIndex() {
        return "false";
    }

    public void setSelectedIndex(String selectedIndex) {
        this.setFullEconSelNum(selectedIndex);
    }

    public void setAltValue2(String key, String value) {
        int pos = Integer.parseInt(key) - 1;
        if (pos >= this.altValue2Coll.size()) {
            for (int i = this.altValue2Coll.size(); i <= pos; i++)
                this.altValue2Coll.add("");
        }
        this.altValue2Coll.setElementAt(value, (Integer.parseInt(key) - 1));
    }

    public String getEvolveFromSel() {
        return evolveFromSel;
    }

    public void setEvolveFromSel(String evolveFromSel) {
        this.evolveFromSel = evolveFromSel;
    }

    public String getEvolveToSel() {
        return evolveToSel;
    }

    public void setEvolveToSel(String evolveToSel) {
        this.evolveToSel = evolveToSel;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public String getVehicleLineSel() {
        return vehicleLineSel;
    }

    public void setVehicleLineSel(String vehicleLineSel) {
        this.vehicleLineSel = vehicleLineSel;
    }

    public Collection<FullEconomicsStruct> getColFullEconomicsStruct() {
        return colFullEconomicsStruct;
    }

    public void setColFullEconomicsStruct(
                                          Collection<FullEconomicsStruct> colFullEconomicsStruct) {
        this.colFullEconomicsStruct = colFullEconomicsStruct;
    }

    public Teesc02Coststvers getCostStrVersion() {
        return costStrVersion;
    }

    public void setCostStrVersion(Teesc02Coststvers costStrVersion) {
        this.costStrVersion = costStrVersion;
    }

    public Timestamp getEconLevel1() {
        return econLevel1;
    }

    public void setEconLevel1(Timestamp econLevel1) {
        this.econLevel1 = econLevel1;
    }

    public String getEconLevel1Forecasted() {
        return econLevel1Forecasted;
    }

    public void setEconLevel1Forecasted(String econLevel1Forecasted) {
        this.econLevel1Forecasted = econLevel1Forecasted;
    }

    public Timestamp getEconLevel2() {
        return econLevel2;
    }

    public void setEconLevel2(Timestamp econLevel2) {
        this.econLevel2 = econLevel2;
    }

    public String getEconLevel2Forecasted() {
        return econLevel2Forecasted;
    }

    public void setEconLevel2Forecasted(String econLevel2Forecasted) {
        this.econLevel2Forecasted = econLevel2Forecasted;
    }

    public FullEconomicsStruct getFullEconomicStructSelected() {
        List<FullEconomicsStruct> aux = (List<FullEconomicsStruct>) this
                .getColFullEconomicsStruct();

        return aux.get(Integer.parseInt(this.getFullEconSelNum()) - 1);
    }

    public FullEconomicsTotalStruct getTotals() {
        return totals;
    }

    public void setTotals(FullEconomicsTotalStruct totals) {
        this.totals = totals;
    }

    public String getEvolveFromYear() {
        return evolveFromYear;
    }

    public void setEvolveFromYear(String evolveFromYear) {
        this.evolveFromYear = evolveFromYear;
    }

    public String getEvolveToYear() {
        return evolveToYear;
    }

    public void setEvolveToYear(String evolveToYear) {
        this.evolveToYear = evolveToYear;
    }

    public String getMixDate() {
        return mixDate;
    }

    public void setMixDate(String mixDate) {
        this.mixDate = mixDate;
    }

    public String getMixYear() {
        return mixYear;
    }

    public void setMixYear(String mixYear) {
        this.mixYear = mixYear;
    }

    public Vector<String> getAltValue2Coll() {
        return altValue2Coll;
    }

    public void setAltValue2Coll(Vector<String> altValue2Coll) {
        this.altValue2Coll = altValue2Coll;
    }

    public String getFullEconSelNum() {
        return fullEconSelNum;
    }

    public void setFullEconSelNum(String fullEconSelNum) {
        this.fullEconSelNum = fullEconSelNum;
    }

    public void reset(ActionMapping mapping, HttpServletRequest request) {
        this.altValue2Coll = new Vector<String>();
    }

    public String getApprovedDate() {
        return approvedDate;
    }

    public void setApprovedDate(String approvedDate) {
        this.approvedDate = approvedDate;
    }

    public String getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(String createdDate) {
        this.createdDate = createdDate;
    }

    public String getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(String lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }

    public String getProposedDate() {
        return proposedDate;
    }

    public void setProposedDate(String proposedDate) {
        this.proposedDate = proposedDate;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    public Teesf01Fulleconom getFullEconomic() {
        return fullEconomic;
    }

    public void setFullEconomic(Teesf01Fulleconom fullEconomic) {
        this.fullEconomic = fullEconomic;
    }

	public Boolean getActivacionNiveles() {
		return activacionNiveles;
	}

	public void setActivacionNiveles(Boolean activacionNiveles) {
		this.activacionNiveles = activacionNiveles;
	}

	public Map<Teesc03Subcoststr, Collection<Teesc07Subcstitem>> getMapaNodosNiveles() {
		return mapaNodosNiveles;
	}

	public void setMapaNodosNiveles(
			Map<Teesc03Subcoststr, Collection<Teesc07Subcstitem>> mapaNodosNiveles) {
		this.mapaNodosNiveles = mapaNodosNiveles;
	}

}
